package com.hntxk.cqzd.diacrisis.service.impl;

import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.core.util.NumberUtil;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisFetusDetail;
import com.hntxk.cqzd.diacrisis.model.DiacrisisAllModel;
import com.hntxk.cqzd.diacrisis.model.FetusAndDetailsModel;
import com.hntxk.cqzd.diacrisis.service.DiacrisisFetusDetailService;
import com.hntxk.cqzd.pacs.model.PacsModel;
import com.hntxk.sp.dict.manager.DictManager;
import com.hntxk.sp.dict.model.Dict;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service("diacFetusDetailService")
@Transactional(rollbackFor = Exception.class)
public class DiacrisisFetusDetailServiceImpl extends BaseServiceImpl<TblDiacrisisFetusDetail> implements DiacrisisFetusDetailService {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.diacrisis.mapper.DiacrisisFetusDetailMapper.";

    public DiacrisisFetusDetailServiceImpl() {
        super(MAPPER_NAMESPACE);
    }


    @Override
    //public ResultModel<FetusAndDetailsModel> insertDetail(FetusAndDetailsModel detail,PacsModel pacsModel) {
    public ResultModel<FetusAndDetailsModel> insertDetail(FetusAndDetailsModel detail) {
        List<TblDiacrisisFetusDetail> fetusDetail = detail.getFetusDetail();
        for(TblDiacrisisFetusDetail diacrisisFetusDetail:fetusDetail){
            /*if (StringUtils.isNotEmpty(pacsModel.getResult())){
                diacrisisFetusDetail.setBConclusion(pacsModel.getResult());
            }*/
            getPrimaryKey(diacrisisFetusDetail);
            setSelectName(diacrisisFetusDetail);
            diacrisisFetusDetail.setFetusId(detail.getFetusId());
            super.insert(MAPPER_NAMESPACE+"insert",diacrisisFetusDetail);
            LogRecordUtil.addLog(new ActionLog(TblDiacrisisFetusDetail.class, diacrisisFetusDetail.getFetusDetailId() + "", "12",
                    ActionType.ADD, "新增B超信息详情：" + diacrisisFetusDetail.getChorionicName() + "[ " + diacrisisFetusDetail.getFetusDetailRemark() + " ]"));
        }
        return ResultModel.empty();
    }

    @Override
    public ResultModel<FetusAndDetailsModel> delete(List<FetusAndDetailsModel> list) {

            for (FetusAndDetailsModel fetusAndDetailsModel : list) {
                delete(MAPPER_NAMESPACE+"deleteByFkId",fetusAndDetailsModel.getFetusId());
            }
            return ResultModel.empty();
    }






    @Override
    public List<TblDiacrisisFetusDetail> getByFkId(FetusAndDetailsModel id) {
        TblDiacrisisFetusDetail fetusDetail=new TblDiacrisisFetusDetail();
            Long long1=id.getFetusId();
        fetusDetail.setFetusId(long1);

        //返回的数组对象
        List<TblDiacrisisFetusDetail> query = (List<TblDiacrisisFetusDetail>) super.query(MAPPER_NAMESPACE + "getByFkId", fetusDetail);
        return query;
    }


    /**
     *
     * @param list
     * @return
     */
    @Override
    public List<FetusAndDetailsModel> getByFkId(List<FetusAndDetailsModel> list) {
        //返回的数组对象
        List<FetusAndDetailsModel> results=new ArrayList<>();

        TblDiacrisisFetusDetail fetusDetail=new TblDiacrisisFetusDetail();
        for (FetusAndDetailsModel fetusAndDetailsModel:list){
            Long long1=fetusAndDetailsModel.getFetusId();
            fetusDetail.setFetusId(long1);

            fetusAndDetailsModel.setFetusDetail((List<TblDiacrisisFetusDetail>) super.query(MAPPER_NAMESPACE+"getByFkId",fetusDetail));
            results.add(fetusAndDetailsModel);
        }

        return results;
    }



    /**
     * 设置下拉name值
     * @param fetusDetail
     */
    public void setSelectName(TblDiacrisisFetusDetail fetusDetail){

        Long siteId = fetusDetail.getSiteId();
        if (siteId!=null) {
            //设置胎盘位置下拉name
            Dict baseSite = DictManager.getInstance().getDict("CQ_BASE_SITE", siteId);
            fetusDetail.setSiteName(baseSite.getText());
        }

        String bAbnormalId = fetusDetail.getBAbnormalId();
        String bAbnormalText="";
        if (bAbnormalId!=null) {
            String[] split = bAbnormalId.split(",");
            for (String s : split) {
                //设置b超异常下拉值
                Dict bAbnormal = DictManager.getInstance().getDict("CQ_BASE_B_ABNORMAL", s);
                bAbnormalText+= bAbnormal.getText()+",";

            }
            fetusDetail.setBAbnormal(bAbnormalText.substring(0,bAbnormalText.length()-1));

        }

    }

}
